﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;

namespace RhythmComet
{
    public interface IPlayingState : IGameState { }

    public sealed class PlayingState : GameState, IPlayingState
    {
        public PlayingState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IPlayingState), this);
        }

        public override void Update(GameTime gameTime)
        {
#if !XBOX
            if (Input.WasPressed(Buttons.Start) || Input.WasPressed(Keys.Space))
#else
            if (Input.WasPressed(Buttons.Start))
#endif
            {
                GameManager.PushState(game.PausedMenuState.Value);
            }

//#if !XBOX
//            if (Input.WasPressed(Keys.Q))
//                game.BeatManager.Band[3].Toggle();

//            if (Input.WasPressed(Keys.W))
//                game.BeatManager.Band[4].Toggle();

//            if (Input.WasPressed(Keys.E))
//                game.BeatManager.Band[5].Toggle();

//            if (Input.WasPressed(Keys.R))
//                game.BeatManager.Band[6].Toggle();

//            if (Input.WasPressed(Keys.T))
//            {
//                game.BeatManager.Band[3].Enabled = true;
//                game.BeatManager.Band[4].Enabled = true;
//                game.BeatManager.Band[5].Enabled = true;
//                game.BeatManager.Band[6].Enabled = true;
//            }
//#endif
#if !XBOX
            if (game.Player.SongFinished())
                GameManager.PushState(game.SongOverState.Value);
#endif

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }

        public override void StateChanged(object sender, EventArgs e)
        {
            base.StateChanged(sender, e);
            if (GameManager.State != this.Value)
            {
                Visible = true;
                Enabled = false;
                game.FrameBurst.Enabled = false;
                game.ObstacleManager.Enabled = false;
                game.BeatManager.Enabled = false;
                game.Comet.Enabled = false;
#if !XBOX
                game.Player.PauseSong();
                game.SpectrumAnalyzer.Enabled = false;
#endif
            }
            else
            {
#if !XBOX
                if (game.Player.IsPlaying == false)
                    game.Player.PlaySong();
                else
                    game.Player.PauseSong();
                game.SpectrumAnalyzer.Visible = true;
                game.SpectrumAnalyzer.Enabled = true;
#endif
                game.ObstacleManager.Visible = true;
                game.ObstacleManager.Enabled = true;
                game.FrameBurst.Visible = true;
                game.FrameBurst.Enabled = true;
                game.BeatManager.Enabled = true;
                game.Comet.Enabled = true;
                game.Comet.Visible = true;
            }
        }
    }
}
